<!DOCTYPE html>
<html>
  <head>
      <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta content="yes" name="apple-mobile-web-app-capable" />
  <meta content="black" name="apple-mobile-web-app-status-bar-style" />
  <meta name="referrer" content="never">
  <meta name="keywords" content="">
  <meta name="description" content="">
  <meta name="author" content="kveln">
  <title>centos7.4配置nfs网络文件共享 | John Wong&#39;s Blog</title>
  <link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
  <!-- <link href="https://blog.jwangkun.com/media/css/bootstrap.min.css" rel="stylesheet"> -->
  <!--  <link href="https://blog.jwangkun.com/media/css/all.min.css" rel="stylesheet" type="text/css"> -->
  <link href="https://cdn.bootcss.com/font-awesome/5.11.2/css/all.min.css" rel="stylesheet">
  <link href='https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
  <link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
  <link rel="alternate" type="application/rss+xml" title="centos7.4配置nfs网络文件共享 | John Wong&#39;s Blog » Feed" href="https://blog.jwangkun.com/atom.xml">
  <link rel="stylesheet"href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.15.10/build/styles/androidstudio.min.css">
  <link href="https://blog.jwangkun.com/styles/main.css" rel="stylesheet">
  <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.15.10/build/highlight.min.js"></script>
  <!-- <script src="https://blog.jwangkun.com/media/scripts/jquery.min.js"></script> -->
  <script>hljs.initHighlightingOnLoad();</script>
  

    <meta property="og:description" content="centos7.4配置nfs网络文件共享"/>
    <meta property="og:url" content="https://blog.jwangkun.com/post/UbCNkIkc8/"/>
    <meta property="og:locale" content="zh-CN"/>
    <meta property="og:type" content="website"/>
    <meta property="og:site_name" content="John Wong&#39;s Blog"/>
  </head>
  <body>
  	<!-- Navigation -->
  <nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
    <div class="container">
      <a class="navbar-brand" href="https://blog.jwangkun.com">John Wong&#39;s Blog</a>
      <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
        Menu
        <i class="fas fa-bars"></i>
      </button>
      <div class="collapse navbar-collapse" id="navbarResponsive">
        <ul class="navbar-nav ml-auto">
          
          <li class="nav-item">
              
              <a class="nav-link" href="/">首页</a>
              
          </li>
          
          <li class="nav-item">
              
              <a class="nav-link" href="/archives">归档</a>
              
          </li>
          
          <li class="nav-item">
              
              <a class="nav-link" href="/tags">标签</a>
              
          </li>
          
          <li class="nav-item">
              
              <a class="nav-link" href="/post/about">关于</a>
              
          </li>
          
        </ul>
      </div>
    </div>
  </nav>
  <!-- Page Header -->
  <header class="masthead" style="background-image: url('https://blog.jwangkun.com/media/images/home-bg.jpg')">
    <div class="overlay"></div>
    <div class="container">
      <div class="row">
        <div class="col-lg-8 col-md-10 mx-auto">
          <div class="post-heading">
          	<span class="tags">
          	 
            <a href="https://blog.jwangkun.com/tag/jY1_HHrkR/" class="tag">NFS</a>
            
            <a href="https://blog.jwangkun.com/tag/GLRJD5xeG0/" class="tag">Linux</a>
            
            <a href="https://blog.jwangkun.com/tag/l-lwQxp4GJ/" class="tag">Centos</a>
            
        </span>
            <h1>centos7.4配置nfs网络文件共享</h1>
            <span class="meta">
            	Posted on
              2020-10-16，6 min read
            </span>
          </div>
        </div>
      </div>
    </div>
  </header>

  <!-- Post Content -->
  <article>
    <div class="container">
      <div class="row">
        <div class="col-lg-8 col-md-10 mx-auto">
          <h3 id="nfs服务端示例ip1021312071"><strong>NFS服务端(示例ip:<strong>10.213.120.71</strong>)</strong></h3>
<p>1 安装nfs</p>
<pre><code class="language-java">[root@nfs_server ~]# yum -y install nfs-utils rpcbind
</code></pre>
<p>（小提示：在安装完nfs-utils后，rpcbind默认是启动了的。）</p>
<p>2 enable services。设置开机启动nfs相关服务。</p>
<pre><code class="language-java">[root@nfs_server ~]# systemctl enable rpcbind
[root@nfs_server ~]# systemctl enable nfs-server
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@nfs_server ~]# systemctl enable nfs-lock
[root@nfs_server ~]# systemctl enable nfs-idmap
</code></pre>
<p>3 启动nfs service</p>
<pre><code class="language-java">[root@nfs_server ~]# systemctl start rpcbind
[root@nfs_server ~]# systemctl start nfs-server
[root@nfs_server ~]# systemctl start nfs-lock
[root@nfs_server ~]# systemctl start nfs-idmap
</code></pre>
<p>4 创建需要共享的目录</p>
<pre><code class="language-java">[root@nfs_server ~]# mkdir -p /data/zbox                    -p 级联创建
[root@nfs_server ~]# chmod -R 777 /data/zbox        更改share文件夹及其子文件夹权限为777
</code></pre>
<p>5 配置需要共享的目录到 /etc/exports下，xxx.xxx.xxx.xxx为需要共享的对象ip地址。</p>
<pre><code class="language-java">[root@nfs_server ~]# echo &quot;/data/zbox 10.213.120.71.*(rw,sync,no_root_squash)&quot; &gt;&gt; /etc/exports
[root@nfs_server ~]# exportfs -rv         使exports的修改生效
</code></pre>
<p>或者</p>
<pre><code class="language-java">[root@nfs_server ~]# echo &quot;/data/zbox 10.213.120.71/24(rw,sync,no_root_squash)&quot; &gt;&gt; /etc/exports
[root@nfs_server ~]# exportfs -rv          使exports的修改生效
</code></pre>
<p>6 检查共享目录是否设置正确</p>
<pre><code class="language-java">[root@nfs_server /]# showmount -e
Export list for nfs_server:
/data/zbox 10.213.120.*
</code></pre>
<p>7 调整防火墙配置</p>
<pre><code>【 111  2049是固定端口】
vim  /etc/services
mountd          20048/tcp
mountd          20048/udp
nfs              2049/tcp
nfs              2049/udp
nlockmgr        29194/tcp --  是动态的，可忽略
nlockmgr        29194/udp
firewall-cmd --add-port=111/tcp --permanent
firewall-cmd --add-port=2049/tcp --permanent
firewall-cmd --add-port=20048/tcp --permanent
firewall-cmd --reload 
</code></pre>
<pre><code class="language-java">[root@nfs_server ~]# firewall-cmd --add-service=nfs --permanent --zone=public
success
[root@nfs_server ~]# firewall-cmd --add-service=mountd --permanent --zone=public
success
[root@nfs_server ~]# firewall-cmd --add-service=rpc-bind --permanent --zone=public
success
[root@nfs_server ~]# firewall-cmd --reload   重新载入配置，使其生效
success
</code></pre>
<p>可使用 命令 <strong>iptables -L -n</strong> 查看开放的端口</p>
<pre><code>target     prot opt source               destination         
cali-INPUT  all  --  0.0.0.0/0            0.0.0.0/0            /* cali:Cz_u1IQiXIMmKD4c */
ACCEPT     udp  --  0.0.0.0/0            169.254.25.10        udp dpt:53
ACCEPT     tcp  --  0.0.0.0/0            169.254.25.10        tcp dpt:53
KUBE-FIREWALL  all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0            udp dpt:53
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:53
ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0            udp dpt:67
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:67
</code></pre>
<p>重启服务</p>
<pre><code>systemctl start rpcbind.service
systemctl start nfs-server.service
</code></pre>
<h3 id="nfs客户端"><strong>NFS客户端</strong></h3>
<p><strong>注意：客户端不需要启动nfs服务</strong></p>
<p>1 安装nfs</p>
<pre><code class="language-java">[root@nfs_client ~]# yum -y install nfs-utils
</code></pre>
<p>2 检查共享目录是否设置正确，xxx.xxx.xxx.xxx 为共享服务器地址</p>
<pre><code class="language-java">[root@nfs_client ~]# showmount -e 10.213.120.71
Export list for 10.213.120.71:
/data/zbox 10.213.120.*
</code></pre>
<p>3 挂载远程服务器NFS分区到本地挂载点</p>
<p># make mount points</p>
<pre><code class="language-java">[root@nfs_client ~]# mkdir -p /data/zbox
</code></pre>
<p># mount nfs</p>
<pre><code class="language-java">[root@nfs_client ~]# mount -t nfs 10.213.120.71:/data/zbox /data/zbox
</code></pre>
<p>挂载格式解读：</p>
<p>mount -t nfs -o nolock,vers=2 10.0.0.147:/work/nfs /mnt</p>
<p>解释一下：<br>
mount :挂载命令<br>
nfs :使用的协议<br>
nolock :不阻塞<br>
vers : 使用的NFS版本号<br>
IP : NFS服务器的IP（NFS服务器运行在哪个系统上，就是哪个系统的IP）<br>
/work/nfs: 要挂载的目录（Ubuntu的目录）<br>
/mnt : 要挂载到的目录（开发板上的目录，注意挂载成功后，/mnt下原有数据将会被隐藏，无法找到）</p>
<p><strong>取消挂载(先df -h 查看分区挂载情况)</strong></p>
<pre><code>[root@localhost local]# umount 10.213.120.71:/data/zbox
</code></pre>
<p><strong>取消挂载方式1</strong></p>
<blockquote>
<p>mount -t nfs 192.168.222.38:/share/api/images /home/work/images</p>
</blockquote>
<p>取消挂载</p>
<pre><code>#挂载
umount 192.168.222.38:/share/api/images
</code></pre>
<p><strong>取消挂载方式2：</strong></p>
<blockquote>
<p>#挂载<br>
mount -t nfs 192.168.222.38:/share/api/images /home/work/images</p>
</blockquote>
<p>取消挂载</p>
<pre><code>umount /home/work/images
</code></pre>
<h3 id="验证nfs网络文件共享是否成功"><strong>验证nfs网络文件共享是否成功：</strong></h3>
<p><strong>切换到nfs_server端：</strong></p>
<pre><code class="language-java">[root@nfs_server ~]# cd /data/zbox/
[root@nfs_server share]# ls
[root@nfs_server share]# touch a.txt
</code></pre>
<p><strong>切换到nfs_client端：</strong></p>
<p>查看挂载是否成功，即查看是否在被挂载目录下，同步生成了 a.txt文件</p>
<pre><code class="language-java">[root@nfs_client ~]# mkdir -p /data/zbox
[root@nfs_client ~]# mount -t nfs 10.213.120.71:/data/zbox /data/zbox
[root@nfs_client ~]# cd /data/zbox/
[root@nfs_client share]# ls
a.txt               可看到在nfs客户端同步生成了a.txt
</code></pre>
<p><em><strong>在客户端使用命令df查看整个的挂载情况</strong></em></p>
<pre><code>[root@nfs_client etc]# df

10.213.120.71:/data/zbox  52403200 15967232  36435968   31% /data/zbox
</code></pre>
<h3 id="注意"><strong>注意：</strong></h3>
<ul>
<li>若服务端挂了，则客户端将无法使用</li>
<li>服务端若设置了开机启动，则每次重启后，服务仍然活着</li>
</ul>
<p>​    客户端重启后，需要重新挂载</p>
<hr>
<h3 id="配置客户端开机自动挂载nfs共享目录"><strong>配置客户端开机自动挂载nfs共享目录</strong></h3>
<p>vi /etc/fstab，增加一行</p>
<pre><code>10.213.120.71:/data/zbox /data/zbox nfs defaults 0 0
</code></pre>
<p>/etc/fstab 里面的挂载配置立即生效的命令</p>
<pre><code class="language-bash">sudo mount -a
</code></pre>
<p>或者在在/etc/rc.d/rc.local中添加一行</p>
<pre><code>mount 10.213.120.71:/data/zbox /data/zbox 
</code></pre>
<pre><code>chmod +x /etc/rc.d/rc.local
</code></pre>
<hr>

          
          <p class="next-post">下一篇：
            <a href="https://blog.jwangkun.com/post/cjiYeW8zk/">
              <span class="post-title">
                k8s使用的web框架：go-restful 源码分析&rarr;
              </span>
            </a>
          </p>
        
        <div class="comment">
          
        </div>
      </div>
    </div>
  </article>
 <!-- Footer -->
  <footer>
    <div class="container">
      <div class="row">
        <div class="col-lg-8 col-md-10 mx-auto">
          <ul class="list-inline text-center">
            
            
              
            
              
            
              
            
              
            
              
            
              
            
              
              <li class="list-inline-item">
              <a href="https://blog.jwangkun.com/atom.xml" target="_blank">
                <span class="fa-stack fa-lg">
                  <i class="fas fa-circle fa-stack-2x"></i>
                  <i class="fas fa-rss fa-stack-1x fa-inverse"></i>
                </span>
              </a>
              </li>
          </ul>
          <p class="copyright text-muted">Copyright &copy;<span>John Wong&#39;s Blog</span><br><a href="https://github.com/getgridea/gridea" class="Themeinfo">Powered by Gridea</a></p>
        </div>
      </div>
    </div>
   </footer>
  <!-- Bootstrap core JavaScript -->
  <script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
  <!-- <script src="https://blog.jwangkun.com/media/scripts/bootstrap.bundle.min.js"></script> -->
  <!-- Bootstrap core JavaScript -->
  <script src="https://cdn.jsdelivr.net/gh/Alanrk/clean-cdn@1.0/scripts/clean-blog.min.js"></script>
  <!-- <script src="https://blog.jwangkun.com/media/scripts/clean-blog.min.js"></script> -->
  <script src="//instant.page/3.0.0" type="module" defer integrity="sha384-OeDn4XE77tdHo8pGtE1apMPmAipjoxUQ++eeJa6EtJCfHlvijigWiJpD7VDPWXV1"></script>
  <style type="text/css">a.back_to_top{text-decoration:none;position:fixed;bottom:40px;right:30px;background:#f0f0f0;height:40px;width:40px;border-radius:50%;line-height:36px;font-size:18px;text-align:center;transition-duration:.5s;transition-propety:background-color;display:none}a.back_to_top span{color:#888}a.back_to_top:hover{cursor:pointer;background:#dfdfdf}a.back_to_top:hover span{color:#555}@media print,screen and(max-width:580px){.back_to_top{display:none!important}}</style>
<a id="back_to_top" href="#" class="back_to_top">
  <span>▲</span></a>
<script>$(document).ready((function(_this) {
    return function() {
      var bt;
      bt = $('#back_to_top');
      if ($(document).width() > 480) {
        $(window).scroll(function() {
          var st;
          st = $(window).scrollTop();
          if (st > 30) {
            return bt.css('display', 'block')
          } else {
            return bt.css('display', 'none')
          }
        });
        return bt.click(function() {
          $('body,html').animate({
            scrollTop: 0
          },
          800);
          return false
        })
      }
    }
  })(this));
  var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?84ab85460bfbe79dbe5776a1df139a8f";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
  </script>
  
<script type="text/javascript" src="https://v1.cnzz.com/z_stat.php?id=1279350888&web_id=1279350888"></script>

  </body>
</html>

